<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>商品管理</title>
  <meta name="renderer" content="webkit">
	<!-- <meta name="referrer" content="no-referrer" /> -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
  <style>
	.layui-form-select dl{ height:200px}
	#ggnamediv .layui-input,#ggvaldiv .layui-input{ display:inline;height:30px}
	.layui-table-view .layui-table td .table-imgbox{width:70px;height:70px;float:left;display: flex;align-items: center;justify-content: center}
	.layui-table-view .layui-table td .table-imgbox img{max-width:70px;max-height:70px;float:left;}
	.layui-option-but button{margin-bottom:10px}
  </style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header">商品管理</div>
          <div class="layui-card-body" pad15>
						<div class="layui-col-md5 layui-option-but">
							{if input('param.showtype')!=2}<a style="margin-bottom:10px" class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="openmax('{:url('edit')}{if input('param.showtype')==21}&bid=-1{/if}{if $fromwxvideo==1}&fromwxvideo=1{/if}')">添加</a>{/if}
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="datadel(0)">删除</button>
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="daoru()">导入</button>
							{if getcustom('product_update_excel')}
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="dataexcel()">导出</button>
							{/if}
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="setst(0,1)">上架</button>
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="setst(0,0)">下架</button>
							{if $cancopy}
								<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="copyProduct(0)">复制商品</button>
							{/if}
							{if $admin['jushuitan_status']==1}
								<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="tongbu()">同步商品</button>
							{/if}
							{if $fromwxvideo==1}
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="towxvideo(0)">上传</button>
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="wxvideoupdatest()">更新</button>
							{/if}
							{if getcustom('product_category_batch_update')}
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="editCategory()">编辑分类</button>
							{/if}
						</div>
						<div class="layui-form layui-form-search layui-col-md7">
								<div class="layui-inline layuiadmin-input-useradmin">
									<label class="layui-form-label">商品名称</label>
									<div class="layui-input-inline">
										<input type="text" name="name" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">{if $bid > 0}平台{/if}分类</label>
									<div class="layui-input-inline">
										<select name="cid">
											<option value="">全部</option>
											{foreach $clist as $cv}
												<option value="{$cv['id']}" {if $cv['id']==input('param.cid')}selected{/if}>{$cv['name']}</option>
												{foreach $cv['child'] as $v}
												<option value="{$v['id']}" {if $v['id']==input('param.cid')}selected{/if}>&nbsp;&nbsp;&nbsp;{$v['name']}</option>
													{foreach $v['child'] as $v3}
													<option value="{$v3['id']}" {if $v3['id']==input('param.cid')}selected{/if}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$v3['name']}</option>
													{/foreach}
												{/foreach}
											{/foreach}
										</select>
									</div>
								</div>
								{if $bid > 0}
								<div class="layui-inline">
									<label class="layui-form-label">商家分类</label>
									<div class="layui-input-inline">
										<select name="cid2">
											<option value="">全部</option>
											{foreach $clist2 as $cv}
												<option value="{$cv['id']}" {if $cv['id']==input('param.cid')}selected{/if}>{$cv['name']}</option>
												{foreach $cv['child'] as $v}
												<option value="{$v['id']}" {if $v['id']==input('param.cid')}selected{/if}>&nbsp;&nbsp;&nbsp;{$v['name']}</option>
												{/foreach}
											{/foreach}
										</select>
									</div>
								</div>
								{/if}
								<div class="layui-inline">
									<label class="layui-form-label">分组</label>
									<div class="layui-input-inline">
										<select name="gid">
											<option value="">全部</option>
											{foreach $glist as $g}<option value="{$g['id']}" {if $g['id']==input('param.gid')}selected{/if}>{$g['name']}</option>{/foreach}
										</select>
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">状态</label>
									<div class="layui-input-inline">
										<select name="status">
											<option value="">全部</option>
											<option value="1">已上架</option>
											<option value="0">未上架</option>
										</select>
									</div>
								</div>
								{if input('param.showtype')==2 || $bid!=0}
								<div class="layui-inline">
									<label class="layui-form-label">审核状态</label>
									<div class="layui-input-inline">
										<select name="ischecked">
											<option value="">全部</option>
											<option value="0">待审核</option>
											<option value="1">已通过</option>
											<option value="2">已驳回</option>
										</select>
									</div>
								</div>
								{/if}
								{if $fromwxvideo==1}
								<div class="layui-inline">
									<label class="layui-form-label">视频号状态</label>
									<div class="layui-input-block">
										<select name="wxvideo_status">
											<option value="">全部</option>
											<option value="0">未上传</option>
											<option value="1">未审核</option>
											<option value="2">审核中</option>
											<option value="3">审核失败</option>
											<option value="4">审核成功</option>
											<option value="5">已上架</option>
											<option value="11">已下架</option>
											<option value="13">违规/风控</option>
										</select>
									</div>
								</div>
								{/if}
								<div class="layui-inline">
									<button class="layui-btn layuiadmin-btn-replys" lay-submit="" lay-filter="LAY-app-forumreply-search">
										<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
									</button>
								</div>
						</div>
						<div class="layui-col-md12">
							<table id="tabledata" lay-filter="tabledata"></table>
						</div>
          </div>
        </div>
    </div>
  </div>
  {if getcustom('product_category_batch_update')}
  <div class="layui-form" lay-filter="" style="margin-top:20px; display: none" id="selectCategory">
	  <div class="layui-form-item">
		  <label class="layui-form-label">商品分类：</label>
		  <div class="layui-input-inline" style="width:400px">
			  <select name="info[cid]" id="cid" xm-select="selectCid" xm-select-max="5" xm-select-search >
				  <option value="">--请选择--</option>
				  {foreach $clist as $cv}
				  <option value="{$cv['id']}" {if in_array($cv['id'],$info['cid'])}selected{/if}>{$cv['name']}</option>
				  {foreach $cv['child'] as $k=>$v}
				  {if $k < count($cv['child'])-1}
				  <option value="{$v['id']}" {if in_array($v['id'],$info['cid'])}selected{/if}>&nbsp;&nbsp;&nbsp;&nbsp;├ {$v['name']}</option>
				  {else}
				  <option value="{$v['id']}" {if in_array($v['id'],$info['cid'])}selected{/if}>&nbsp;&nbsp;&nbsp;&nbsp;└ {$v['name']}</option>
				  {/if}
				  {foreach $v['child'] as $k2=>$v2}
				  {if $k2 < count($v['child'])-1}
				  <option value="{$v2['id']}" {if in_array($v2['id'],$info['cid'])}selected{/if}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├ {$v2['name']}</option>
				  {else}
				  <option value="{$v2['id']}" {if in_array($v2['id'],$info['cid'])}selected{/if}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└ {$v2['name']}</option>
				  {/if}
				  {/foreach}
				  {/foreach}
				  {/foreach}
			  </select>
		  </div>
		  <div class="layui-form-mid layui-word-aux">最多可选5个分类</div>
		  <!-- <div class="layui-form-mid layui-word-aux"><a href="categoryadd.php">创建分类</a></div> -->
	  </div>
  </div>
  {/if}
	{include file="public/js"/}
	<script>
  var table = layui.table;
	var datawhere = {};
	// 图片懒加载必须使用
	layui.use('flow', function(){
		var flow = layui.flow;
		flow.lazyimg();
	});
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: "{$Request.url}" //数据接口
    ,page: true //开启分页
	,limits:[10,20,50,100,200]
    ,cols: [[ //表头
			{type:"checkbox"},
      {field: 'id', title: 'ID',  sort: true,width:80},
			{if input('param.showtype')==2}
      {field: 'bname', title: '所属商户',width:160},
			{/if}
      {field: 'name', title: '商品信息',width:320,templet:function(d){
				var html = '';
				html += '<div class="table-imgbox"><img lay-src="'+d.pic+'" src="__STATIC__/admin/layui/css/modules/layer/default/loading-2.gif"/></div>';
				html += '<div style="float: left;width:200px;margin-left: 10px;white-space:normal;line-height:20px;">';
				html += '	<div style="width:100%;">'+(d.cid!=0?'<span style="color:red">['+d.cname+']'+(d.cname2?'['+d.cname2+']':'')+'</span>':'')+d.name+'</div>';
				{if getcustom('price_dollar')}
					if(d.usdprice>0){
						html += '	<div style="padding-top:5px;color:#f60;float:left;margin-right:10px">$'+d.usdprice+'</div>';	
					}
				{/if}
				if(d.price_type == 1)
					html += '	<div style="padding-top:5px;color:#f60">询价</div>';
				else
					html += '	<div style="padding-top:5px;color:#f60">￥'+d.sell_price+'</div>';
				html += '</div>';
				return html;
			}},
	  {if getcustom('product_stock_warning')}
		   {field: 'ggdata', title: '商品规格',sort: true},
	  {/if}
      {field: 'stock', title: '库存',sort: true},
      {field: 'sales', title: '显示销量',sort: true},
      {field: 'realsalenum', title: '实际销量'},
      {if $fromwxvideo!=1}{field: 'sort', title: '序号',sort: true},{/if}
      {field: 'createtime', title: '创建时间',sort: true,templet:function(d){ return date('Y-m-d H:i',d.createtime)}},
      {field: 'status', title: '商品状态',templet:function(d){ return d.status==1?'<span style="color:green">已上架</span>':'<span style="color:red">未上架</span>'},width:100},
			{if input('param.showtype')==2 || $bid!=0}
			{field: 'status', title: '审核状态',templet:function(d){ 
				if(d.ischecked==0) return '<span style="color:blue">待审核</span>';
				if(d.ischecked==1) return '<span style="color:green">已通过</span>';
				if(d.ischecked==2) return '<span style="color:red">已驳回</span>';
			},width:100},
			{/if}
			{if $fromwxvideo==1}
			{field: 'status', title: '审核状态',templet:function(d){ 
				if(d.wxvideo_product_id=='') return '<button class="table-btn" style="color:#999;">未上传</button>';
				if(d.wxvideo_edit_status==0) return '<button class="table-btn" style="color:#999;">初始状态</button>';
				if(d.wxvideo_edit_status==1) return '<button class="table-btn" style="color:blue">未审核</button>';
				if(d.wxvideo_edit_status==2) return '<button class="table-btn" style="color:blue" onclick="wxvideo_del_audit(\''+d.id+'\')">审核中</button>';
				if(d.wxvideo_edit_status==3) return '<button class="table-btn" style="color:red" onclick="viewrejectReason(\''+d.id+'\')">审核失败</button>';
				if(d.wxvideo_edit_status==4) return '<button class="table-btn" style="color:green">审核成功</button>';
				return '';
			},width:100},
			{field: 'status', title: '上架状态',templet:function(d){ 
				if(d.wxvideo_status==0) return '<button class="table-btn" style="color:#999;">未上架</button>';
				if(d.wxvideo_status==5) return '<button class="table-btn" style="color:green" onclick="wxvideo_delisting(\''+d.id+'\')">已上架</button>';
				if(d.wxvideo_status==11) return '<button class="table-btn" style="color:red" onclick="wxvideo_listing(\''+d.id+'\')">已下架</button>';
				if(d.wxvideo_status==13) return '<button class="table-btn" style="color:red">违规/风控</button>';
				return '';
			},width:100},
			{/if}
      {field: 'operation', title: '操作',templet:function(d){
				if('{$bid}'!=0 && d.linkid>0 && !d.iscustomoption){
					return '';
				}
				var html = '';
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){showqr('+d.id+')})">查看链接</button>';
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){procopy('+d.id+')})">复制</button>';
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){openmax(\'{:url('edit')}/id/'+d.id+'{if $fromwxvideo==1}&fromwxvideo=1{/if}\')})">编辑</button>';
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){datadel('+d.id+')})">删除</button>';
				if(d.status == 0){
					html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){setst('+d.id+',1)})">上架</button>';
				}else{
					html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){setst('+d.id+',0)})">下架</button>';
				}
				if(d.freighttype == 4){
					html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){openmax(\'{:url('ShopCode/codelist')}/proid/'+d.id+'/isopen/1\')})">卡密信息</button>';
				}
				if('{$bid}'==0 && d.bid>0){
					if(d.ischecked == 0 || d.ischecked == 2){
						html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){setcheckst(\''+d.id+'\',1)})">通过</button>';
					}
					if(d.ischecked == 0 || d.ischecked == 1){
						html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){setcheckst(\''+d.id+'\',2)})">驳回</button>';
					}
				}
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){changeprice('+d.id+')})">改价</button>';
				{if getcustom('guige_split')}
				html += '<button class="table-btn" onclick="checklock('+d.islock+',function(){guige_split('+d.id+')})">规格拆分</button>';
				{/if}
				{if getcustom('product_weight')}
					if(d.product_type==2){
						html += '<button class="table-btn" onclick="openmax(\'{:url('ShCustomerPrice/add')}/isopen/1/proid/'+d.id+'\')">客户定价</button>';
					}
				{/if}
				
				{if getcustom('edit_locking') && session('IS_ADMIN')>0}
				if(d.islock == 1){
					html += '<button class="table-btn" onClick="checklock('+d.islock+',function(){dolock('+d.id+',0)})">解锁</button>';
				}else{
					html += '<button class="table-btn" onClick="dolock('+d.id+',1)">锁定</button>';
				}
				{/if}
				{if getcustom('product_mendian_hexiao_givemoney')}
					html += '<button class="table-btn" onclick="openmax(\'{:url('mendian_hexiao_set')}/proid/'+d.id+'\')" >设置门店提成</button>';
				{/if}
				return html;
      }}
    ]],
		// 监听表格的数据更新
		done: function(res, curr, count){
			if(document.documentElement.scrollTop >= 300){
				document.documentElement.scrollTop = document.documentElement.scrollTop-1
			}else{
				document.documentElement.scrollTop = 5
				if(document.documentElement.scrollTop>0){
				}else{
					$(".layui-table-body .table-imgbox").each(function(){
						 var src = $(this).find('img').attr('lay-src');
						 $(this).find('img').attr('src',src)
					})
				}
			}
		}
  });
	// 查看链接
	function showqr(id){
		var pagepath = 'pages/shop/product?id='+id;
		viewLink(pagepath); 
	}
	// 家庭
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	function procopy(id){
		layer.confirm('确定要复制该商品吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('procopy')}",{id:id},function(data){
				layer.close(index);
				if(data.status == 1){
					tableIns.reload();
					layer.confirm('复制成功，是否前去编辑？', {icon: 1, title:'提示',btn:['是','否']}, function(index){
						layer.close(index);
						openmax("{:url('edit')}/id/"+data.proid);
					},function(index){
						layer.close(index);
					});
				}else{
					dialog(data.msg,data.status);
				}
			})
		});
	}
	
	//审核
	function setcheckst(id,st){
		if(st == 2){
			var html = '';
			html+='	<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
			html+='		<label class="layui-form-label" style="width:80px">驳回原因</label>';
			html+='		<div class="layui-input-inline" style="width:350px">';
			html+='			<textarea type="text" id="check_reason" class="layui-textarea"></textarea>';
			html+='		</div>';
			html+='	</div>';
			var checkLayer = layer.open({type:1,area:['500px','250px'],title:false,content:html,shadeClose:true,btn: ['确定', '取消'],
				yes:function(){
					var index = layer.load();
					$.post("{:url('setcheckst')}",{id:id,st:st,reason:$('#check_reason').val()},function(res){
						layer.close(index);
						dialog(res.msg,res.status);
						layer.close(checkLayer);
						tableIns.reload()
					})
				}
			})
		}else{
			layer.confirm('确定要审核通过吗?',{icon: 7, title:'操作确认'}, function(index){
				layer.close(index);
				var index = layer.load();
				$.post("{:url('setcheckst')}",{id:id,st:st},function(data){
					layer.close(index);
					dialog(data.msg,data.status);
					tableIns.reload()
				})
			});
		}
	}
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	//上下架
	function setst(id,st){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要'+(st==0?'下架':'上架')+'吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('setst')}",{ids:ids,st:st},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}

	//改价
	function changeprice(proid){
		var index = layer.load();
		$.post("{:url('getproduct')}",{proid:proid},function(data){
			layer.close(index);
			var product = data.product;
			var specs = data.guigedata;
			var gglist = data.gglist;
			var len = specs.length;
			var newlen = 1; 
			var h = new Array(len); 
			var rowspans = new Array(len); 
			var html = '<div style="margin:10px" class="layui-form" lay-filter="changepriceForm"><table id="ggvaldiv" class="layui-table"><thead><tr>';
			for(var i=0;i<len;i++){
				html+="<th>" + specs[i].title + "</th>";
				var itemlen = specs[i].items.length;
				if(itemlen<=0) { itemlen = 1 };
				newlen*=itemlen;
				h[i] = new Array(newlen);
				for(var j=0;j<newlen;j++){
					h[i][j] = new Array();
				}
				var l = specs[i].items.length;
				rowspans[i] = 1;
				for(j=i+1;j<len;j++){
					rowspans[i]*= specs[j].items.length;
				}
			}
			html += '<th>市场价（元）</th>';
			html += '<th>成本价（元）</th>';
			if(product.lvprice==1){
				{foreach $levellist as $level}
				html += '<th>{$level.name}（元）</th>';
				{/foreach}
			}else{
				html += '<th>销售价（元）</th>';
			}
			html += '<th>库存</th>';
			html += '</tr></thead>';
			
			for(var m=0;m<len;m++){
				var k = 0,kid = 0,n=0;
				for(var j=0;j<newlen;j++){
					var rowspan = rowspans[m]; 
					if( j % rowspan==0){
						h[m][j]={ k:specs[m].items[kid].k,title: specs[m].items[kid].title, html: "<td rowspan='" +rowspan + "'>"+ specs[m].items[kid].title+"</td>\r\n",id: specs[m].items[kid].id};
					}else{
						h[m][j]={ k:specs[m].items[kid].k,title:specs[m].items[kid].title, html: "",id: specs[m].items[kid].id};	
					}
					n++;
					if(n==rowspan){
						kid++; if(kid>specs[m].items.length-1) { kid=0; }
						n=0;
					}
				}
			}
			var hh = "";
			for(var i=0;i<newlen;i++){
				hh+="<tr>";
				var ks = [];
				var titles = [];
				for(var j=0;j<len;j++){
					hh+=h[j][i].html; 
					ks.push( h[j][i].k);
					titles.push( h[j][i].title);
				}
				ks =ks.join(',');
				titles =titles.join(',');
				if(typeof(gglist[ks])!='undefined'){
					var val = gglist[ks];
				}else{
					var val = { procode:'',market_price:'',cost_price:'',sell_price:'',weight:'',stock:'1000',pic:''};
				}

				hh += '<td>';
				hh += '<input name="option['+ks+'][ggid]" type="hidden" value="'+(val.id)+'"/>';
				hh += '<input name="option['+ks+'][market_price]" type="text" class="layui-input" style="width:70px" value="'+(val.market_price==null?'':val.market_price)+'"/>';
				if(i==0){
					hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plset(\'market_price\')"></i>';
				}
				hh += '</td>';
				hh += '<td>';
				hh += '	<input name="option['+ks+'][cost_price]" type="text" class="layui-input" style="width:70px" value="'+(val.cost_price==null?'':val.cost_price)+'"/>';
				if(i==0){
					hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plset(\'cost_price\')"></i>';
				}
				hh += '</td>';
				if(product.lvprice==1){
					val.lvprice_data = JSON.parse(val['lvprice_data']);
					{foreach $levellist as $lk=>$level}
					hh += '<td>';
					hh += '	<input name="option['+ks+'][sell_price_{$level.id}]" type="text" style="width:70px" value="'+(val.lvprice_data==null || val.lvprice_data[{$level.id}]==null?'':val.lvprice_data[{$level.id}])+'" class="layui-input" id="levelprice_'+i+'_{$lk}"/>';
					if(i==0){
						{if $lk==0}
						hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plset(\'sell_price_{$level.id}\')"></i>';
						{else}
						hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plsetlevelprice(\''+i+'\',\'{$lk}\')"></i>';
						{/if}
					}
					hh += '</td>';
					{/foreach}
				}else{
					hh += '<td>';
					hh += '	<input name="option['+ks+'][sell_price]" type="text" style="width:70px" value="'+(val.sell_price==null?'':val.sell_price)+'" class="layui-input"/>';
					if(i==0){
						hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plset(\'sell_price\')"></i>';
					}
					hh += '</td>';
				}
				hh += '<td>';
				hh += ' <input name="option['+ks+'][stock]" type="text" style="width:60px" value="'+(val.stock==null?'':val.stock)+'" class="layui-input"/>';
				if(i==0){
					hh += ' <i class="fa fa-hand-o-down" style="cursor:pointer" title="批量设置" onclick="plset(\'stock\')"></i>';
				}
				hh += '</td>';
				hh += "</tr>";
			}
			html+=hh;
			html+='</table></div>';
			layer.open({type:1,title:product.name,content:html,area:['900px','600px'],shadeClose:true,btn:['确定','取消'],
				yes:function(index){
					layer.close(index);
					var formdata = layui.form.val("changepriceForm");
					formdata['proid'] = proid;
					console.log(formdata)
					var index = layer.load();
					$.post("{:url('changeprice')}",formdata,function(data){
						layer.close(index);
						dialog(data.msg,data.status);
						tableIns.reload()
					})
				}
			});
		});
	}
	function plset(name){
		$("input[name$='["+name+"]']").val($("input[name$='["+name+"]']").eq(0).val());
	}
  function plsetlevelprice(i,k){
	  var thisprice = $('#levelprice_'+i+'_'+k).val();
	  console.log(thisprice)
	  var level0price = $('#levelprice_0_0').val();
	  console.log(level0price)
	  var pricepercent = thisprice / level0price;
	  console.log(pricepercent)
	  $("input[name$='][market_price]']").each(function(i2,v2){
		  var this_price = ($('#levelprice_'+i2+'_0').val() * pricepercent).toFixed(2);
		  console.log(this_price)
		  this_price = parseInt(this_price*100)/100;
		  $('#levelprice_'+i2+'_'+k).val(this_price);
	  })
  }
	//批量编辑分类
	function editCategory(){
			var ids = [];
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择需要编辑的商品');
			}
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
			var checkLayer = layer.open({type:1,area:['600px','400px'],title:'批量编辑分类',content:$("#selectCategory"),shadeClose:true,btn: ['确定', '取消'],
				yes:function(){
					var cid = 	$("input[name='info[cid]']").val(); 
					if(!cid){
						dialog('请选择分类');
						return false;
					}
					var index = layer.load();
					$.post("{:url('editManyCategory')}",{ids:ids,cid:cid},function(res){
						layer.close(index);
						dialog(res.msg,res.status);
						layer.close(checkLayer);
						tableIns.reload();
					})
				}
			});
			layui.form.render();
	
	}
  //上传商品到视频号
  function towxvideo(id){
	  var ids = [];
	  if(id==0){
		  var checkStatus = table.checkStatus('tabledata')
		  var checkData = checkStatus.data; //得到选中的数据
		  if(checkData.length === 0){
			  return layer.msg('请选择数据');
		  }
		  for(var i=0;i<checkData.length;i++){
			  ids.push(checkData[i]['id']);
		  }
	  }else{
		  ids.push(id)
	  }
	  var html = '';
	  html += '<div class="layui-form" lay-filter="" style="margin-top:20px">';
	  html += '<div class="layui-form-item">';
	  html += '	<label class="layui-form-label" style="width:120px">类目ID：</label>';
	  html += '	<div class="layui-input-inline" style="width:200px">';
	  html += '		<input type="text" name="info[third_cat_id]" id="third_cat_id" lay-verify="required" lay-verType="tips" class="layui-input" value="">';
	  html += '	</div>';
	  html += '	<button type="button" class="layui-btn layui-btn-primary" onclick="showChooseCategory()">选择</button>';
	  html += '</div>';
	  html += '<div class="layui-form-item">';
	  html += '	<label class="layui-form-label" style="width:120px">品牌：</label>';
	  html += '	<div class="layui-input-inline" style="width:200px">';
	  html += '		<select name="info[brand_id]" id="brand_id">';
	  {foreach $brand_list as $v}
	  html += '			<option value="{$v.brand_id}">{$v.brand_wording}</option>';
	  {/foreach}
		  html += '		</select>';
		  html += '	</div>';
		  html += '</div>';

		  html += '<div class="layui-form-item" id="trademark_registration_certificate_div">';
		  html += '	<label class="layui-form-label" style="width:120px">商品资质图片：</label>';
		  html += '	<input type="hidden" name="info[qualification_pics]" value="" id="qualification_pics">';
		  html += '	<button style="float:left;" type="button" class="layui-btn layui-btn-primary" onclick="uploader(this,true)" upload-input="qualification_pics" upload-preview="qualification_picsList" >上传图片</button>';
		  html += '	<div class="layui-form-mid layui-word-aux" style="margin-left:10px;">如所选类目需要商品资质，请在此上传</div>';
		  html += '	<div id="qualification_picsList" style="float:left;padding-top:10px;padding-left:150px;clear:both">';
		  html += '	</div>';
		  html += '</div>';
		  html += '</div>';
		  var checkLayer = layer.open({type:1,area:['600px','400px'],title:'上传商品到视频号',content:html,shadeClose:true,btn: ['确定', '取消'],
			  yes:function(){
				  var third_cat_id = $('#third_cat_id').val();
				  var qualification_pics = $('#qualification_pics').val();
				  var brand_id = $('#brand_id').val();
				  if(!third_cat_id || !brand_id){
					  dialog('请选择类目和品牌');
					  return false;
				  }
				  var index = layer.load();
				  $.post("{:url('towxvideo')}",{ids:ids,third_cat_id:third_cat_id,qualification_pics:qualification_pics,brand_id:brand_id},function(res){
					  layer.close(index);
					  dialog(res.msg,res.status);
					  layer.close(checkLayer);
					  tableIns.reload();
				  })
			  }
		  });
		  layui.form.render();
	  }
	function showChooseCategory(){
		layer.open({type:2,title:'选择类目',content:"{:url('Wxvideo/allcategory')}&ischoose=1",area:['900px','600px'],shadeClose:true});
	}
	function chooseCategory(data){
		console.log(data)
		$("input[name='info[third_cat_id]']").val(data.third_cat_id);
	}
	//复制商品
	function copyProduct(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择要复制的商品');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
	
		//选择账号
		var html = '<div style="margin:20px auto;">';
		html+='<div class="layui-form" lay-filter="">';
			{if getcustom('admin_user_group')}
			html+='<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
			html+='	<label class="layui-form-label" style="width:150px">选择分组</label>';
			html+='	<div class="layui-input-inline" style="width:180px">';
			html+='		<select id="togroupid" name="togroupid" style="width:100%;height: 38px;line-height: 38px;border:1px solid #e6e6e6;background-color: #fff;border-radius: 2px;">';
			html+='			<option value="">请选择分组</option>';
			{foreach $groupArr as $k=>$v}
			html+='			<option value="{$k}">{$v}</option>';
			{/foreach}
			html+='		</select>';
			html+='	</div>';
			html+='</div>';
			{/if}
		html+='<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='	<label class="layui-form-label" style="width:150px">选择账号</label>';
		html+='	<div class="layui-input-inline" style="width:180px">';
		html+='		<select id="toaid" name="toaid" style="width:100%;height: 38px;line-height: 38px;border:1px solid #e6e6e6;background-color: #fff;border-radius: 2px;" lay-filter="changeuserid">';
		html+='			<option value="">请选择账号</option>';
		{foreach $userlist as $k=>$v}
		html+='			<option value="{$v["aid"]}">{$v["un"]}</option>';
		{/foreach}
		html+='		</select>';
		html+='	</div>';
		html+='</div>';
		
		html+='<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='	<label class="layui-form-label" style="width:150px">选择分类</label>';
		html+='	<div class="layui-input-inline" style="width:180px">';
		html+='		<select id="tocid" name="tocid" style="width:100%;height: 38px;line-height: 38px;border:1px solid #e6e6e6;background-color: #fff;border-radius: 2px;">';
		html+='		</select>';
		html+='	</div>';
		html+='</div>';

		html+='</div>';
		html+='</div>'
		var copyLayer = layer.open({type:1,title:'复制商品到其他账号',area:['500px','500px'],content:html,shadeClose:true,btn: ['确定', '取消'],
			yes:function(){
				var index = layer.load();
				layer.confirm('确定要复制到该账号吗?',{icon: 7, title:'操作确认'}, function(index){
					//do something
					layer.close(index);
					var index = layer.load();
					var togroupid = $('#togroupid').val();
					var toaid = $('#toaid').val();
					var tocid = $('#tocid').val();
					$.post("{:url('userProcopy')}",{ids:ids,toaid:toaid,tocid:tocid,togroupid:togroupid},function(data){
						layer.close(index);
						if(data.status == 1){
							dialog(data.msg,data.status);
							layer.close(copyLayer);
						}else{
							dialog(data.msg,data.status);
						}
					})
				},function(){
					layer.close(index);
				});
			}
		})
		layui.form.render();
		layui.form.on('select(changeuserid)', function(obj){
			//var field = obj.field;
			var toaid = obj.value;
			var index= layer.load();
			$.post("{:url('getcategory')}",{toaid:toaid},function(res){
				layer.close(index);
				var clist = res.data;
				var html = '';
				for(var i in clist){
					html += '<option value="'+clist[i].id+'">'+clist[i].name+'</option>';
					if(clist[i].child){
						for(var j in clist[i].child){
							html += '<option value="'+clist[i].child[j].id+'">&nbsp;&nbsp;&nbsp;'+clist[i].child[j].name+'</option>';
							for(var k in clist[i].child[j].child){
								html += '<option value="'+clist[i].child[j].child[k].id+'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+clist[i].child[j].child[k].name+'</option>';
							}
						}
					}
				}
				$('#tocid').html(html);
				layui.form.render();
			})
		})

	}

				var pagenum = 1;
				var pagelimit = 1;
	//导入
	function daoru(){
		var html = '<div style="margin:20px auto;">';
		html+='<div class="layui-form" lay-filter="">';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">上传EXCEL文件：</label>';
		html+='		<div class="layui-input-inline" style="width:300px">';
		html+='			<input type="text" name="upload_file" id="upload_file" class="layui-input">';
		html+='		</div>';
		html+='		<button style="float:left;" type="button" class="layui-btn layui-btn-primary uploadexcel" upload-input="upload_file">上传</button>';
		html+='	</div>';
		html+='	<div class="layui-form-item" style="padding:0 20px">';
		html+='		<div class="layui-form-mid" style="color:red;">请按导入格式进行导入</div>';
		html+='	</div>';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px"></label>';
		var demo = '<div class="layui-form-mid"> <a href="__STATIC__/demo_product.xlsx">点击下载查看导入格式</a></div>';
		{if getcustom('product_field_buy')}
			demo ='		<div class="layui-form-mid"> <a href="__STATIC__/demo_product2.xlsx">点击下载查看导入格式</a></div>';
		{/if}
		{if getcustom('product_update_excel')}
			demo ='		<div class="layui-form-mid"> <a href="__STATIC__/demo_product3.xlsx">点击下载查看导入格式</a></div>';
		{/if}
		html += demo;
		html+='	</div>';
		html+='	<div class="layui-form-item" style="margin-top:30px">';
		html+='		<label class="layui-form-label" style="width:150px"></label>';
		html+='		<div class="layui-input-inline">';
		html+='			<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit_excel">确定导入</button>';
		html+='		</div>';
		html+='	</div>';
		html+='</div>';
		html+='</div>'
		layer.open({type:1,area: ['600px', '360px'],title:'导入商品',content:html,shadeClose:true})
		layui.form.render();
		//文件上传
		layui.upload.render({
			elem: '.uploadexcel',
			accept:'file',
			url: "{:url('upload/index')}",
			done: function(res){
				if(res.status==0){
					dialog(res.msg,0);
				}else{
					var item = this.item;
					var inputid = $(this.item).attr('upload-input');
					if(inputid){
						$('#'+inputid).val(res.url);
						$('#'+inputid).change();
					}
				}
			}
		});
		layui.form.on('submit(submit_excel)', function(obj){
			var field = obj.field;
			console.log(field);
			var index= layer.load();
			var url = "{:url('importexcel')}";
			pagenum = 1;

			{if getcustom('product_update_excel')}
			url = "{:url('importexcelnew')}";
			{/if}
			_tplsend(field,url);

		})
	}
			function _tplsend(field,url){

				console.log(pagenum);
				var index = layer.load();

				$.ajax({
					type:'POST',
					url:url+"/pagenum/"+pagenum+'/pagelimit/'+pagelimit,
					dataType:'json',
					data:{file:field.upload_file},
					success:function(data){
						layer.close(index);
						if(data.status==1){
							pagenum++;
							if(data.status==1){
								layer.msg(data.msg,{offset:'100px'});
							}
							if(data.status==1){
								_tplsend(field,url);
							}else{
								dialog(data.msg,data.status);
								setTimeout(function(){
									layer.closeAll();
									tableIns.reload()
								},1000);
							}
						}else{
							dialog(data.msg,data.status);
							setTimeout(function(){
								layer.closeAll();
								tableIns.reload()
							},1000);
						}
					},
					error:function(){
						layer.close(index);
						dialog('未知错误',0);
					}
				})
			}
	function wxvideoupdatest(){
		layer.confirm('当出现状态不一致时可使用此功能，是否更新全部商品的视频号状态?',function(){
			layer.close(index);
			var index = layer.load();
			$.post("{:url('wxvideoupdatest')}",{},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	//撤回审核
	function wxvideo_del_audit(proid){
		layer.confirm('是否要撤回审核?',function(){
			layer.close(index);
			var index = layer.load();
			$.post("{:url('wxvideo_del_audit')}",{proid:proid},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	//视频号上架
	function wxvideo_listing(proid){
		layer.confirm('是否要上架?',function(){
			layer.close(index);
			var index = layer.load();
			$.post("{:url('wxvideo_listing')}",{proid:proid},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	//视频号下架
	function wxvideo_delisting(proid){
		layer.confirm('是否要下架?',function(){
			layer.close(index);
			var index = layer.load();
			$.post("{:url('wxvideo_delisting')}",{proid:proid},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	function viewrejectReason(proid){
		var index = layer.load();
		$.post("{:url('getproduct')}",{proid:proid},function(data){
			layer.close(index);
			layer.open({type:1,area:['300px','300px'],content:'<div style="margin:auto auto;word-break:break-word;padding:20px">'+data.product.wxvideo_reject_reason+'</div>',title:false,shadeClose:true})
		});
	}
	//规格拆分
	var gglist;
	function guige_split(proid){
		var index = layer.load();
		$.post("{:url('getsplitdata')}",{proid:proid},function(res){
			layer.close(index);
			var splitlist = res.splitlist;
			gglist = res.gglist;
			var html = '<div style="margin:20px" class="layui-form">';
			html+='<div id="ggsplit_div">';
			for(var i in splitlist){
				html+='<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
				html+='	<label class="layui-form-label">拆分公式</label>';
				html+='	<div class="layui-input-inline" style="width:180px">';
				html+='		<select name="ggid1[]">';
				html+='			<option value="">请选择规格</option>';
				for(var j in gglist){
					html+='		<option value="'+gglist[j]['id']+'" '+(splitlist[i].ggid1 == gglist[j]['id'] ? 'selected' : '')+'>'+gglist[j]['name']+'</option>';
				}
				html+='		</select>';
				html+='	</div>';
				html+='	<div class="layui-form-mid"> = </div>';
				html+='	<div class="layui-input-inline" style="width:180px">';
				html+='		<select name="ggid2[]">';
				html+='			<option value="">请选择规格</option>';
				for(var j in gglist){
					html+='		<option value="'+gglist[j]['id']+'" '+(splitlist[i].ggid2 == gglist[j]['id'] ? 'selected' : '')+'>'+gglist[j]['name']+'</option>';
				}
				html+='		</select>';
				html+='	</div>';
				html+='	<div class="layui-form-mid"> × </div>';
				html+='	<div class="layui-input-inline" style="width:70px">';
				html+='		<input type="number" name="multiple[]" class="layui-input" value="'+splitlist[i].multiple+'">';
				html+='	</div>';
				if(i == 0){
					html+='<button type="button" class="layui-btn layui-btn-primary" onclick="addggsplit()">添加</button>';
				}else{
					html+='<button type="button" class="layui-btn layui-btn-primary" onclick="delggsplit(this)">删除</button>';
				}
				html+='</div>';
			}
			html+='</div>';
			html+='<input type="hidden" name="proid" value="'+proid+'"/>';

			html+='<div class="layui-form-item" style="margin-top:50px">';
			html+='	<label class="layui-form-label"></label>';
			html+='	<div class="layui-input-inline">';
			html+='		<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit_set_ggsplit">确 定</button>';
			html+='	</div>';
			html+='</div>';
			html+='</div>';
			
			layer.open({type:1,area:['800px','600px'],content:html,title:'规格拆分',shadeClose:true});
			layui.form.render();
			layui.form.on('submit(submit_set_ggsplit)', function(obj){
				var field = obj.field
				var index = layer.load();
				$.post("{:url('ggsplit')}",field,function(data){
					layer.close(index);
					dialog(data.msg,data.status,data.url);
				});
			});
		});
	}
	function addggsplit(){
		var html='<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='	<label class="layui-form-label">拆分公式</label>';
		html+='	<div class="layui-input-inline" style="width:180px">';
		html+='		<select name="ggid1[]">';
		html+='			<option value="">请选择规格</option>';
		for(var j in gglist){
			html+='		<option value="'+gglist[j]['id']+'">'+gglist[j]['name']+'</option>';
		}
		html+='		</select>';
		html+='	</div>';
		html+='	<div class="layui-form-mid"> = </div>';
		html+='	<div class="layui-input-inline" style="width:180px">';
		html+='		<select name="ggid2[]">';
		html+='			<option value="">请选择规格</option>';
		for(var j in gglist){
			html+='		<option value="'+gglist[j]['id']+'">'+gglist[j]['name']+'</option>';
		}
		html+='		</select>';
		html+='	</div>';
		html+='	<div class="layui-form-mid"> × </div>';
		html+='	<div class="layui-input-inline" style="width:70px">';
		html+='		<input type="number" name="multiple[]" id="third_cat_id" class="layui-input" value="">';
		html+='	</div>';
		html+='	<button type="button" class="layui-btn layui-btn-primary" onclick="delggsplit(this)">删除</button>';
		html+='</div>';
		$('#ggsplit_div').append(html);
		layui.form.render();
	}
	function delggsplit(obj){
		$(obj).parent().remove();
	}
	function dolock(id,st){
		layer.confirm('确定要'+(st==1?'锁定':'解除锁定')+'吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('dolock')}",{id:id,st:st},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	function checklock(islock,func){
		if(islock == 0){
			func();return;
		}
		var html = '';
		html+='	<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='		<label class="layui-form-label" style="width:140px">输入解锁密码：</label>';
		html+='		<div class="layui-input-inline" style="width:200px">';
		html+='			<input type="password" id="lockpwd" class="layui-input"/>';
		html+='		</div>';
		html+='	</div>';
		var openmaxneedpwdLayer = layer.open({type:1,area:['600px','200px'],title:false,content:html,shadeClose:true,btn: ['确定', '取消'],
			yes:function(){
				var index = layer.load();
				$.post("{:url('inputlockpwd')}",{lockpwd:$('#lockpwd').val()},function(data){
					layer.close(index);
					if(data.status == 1){
						layer.close(openmaxneedpwdLayer);
						func();
					}else{
						dialog(data.msg,data.status);
					}
				});
			}
		});
	}
	function tongbu(){
		layer.confirm('如果商品较多,同步时间会比较长,请耐心等待,确定要同步吗?',{icon: 7, title:'从聚水潭同步商品'}, function(index){
			layer.close(index)
			infonum = 0;
			next_openid = '';
			totalpage = 0;
			getopenidnum = 0;
			openids = [];
			countopenid = 0;
			loadingdialog = layer.open({type:1,content:'<div style="padding:10px 5px"><div class="flex-y-center"><img src="__STATIC__/admin/img/loading.gif"/><span style="font-size:20px;padding-left:5px">正在同步请勿操作...</span></div></div>',area:'300px',title:false,closeBtn:false});
			$.post("{:url('jsttongbu')}",{ op:'getpage'},function(data){
				if(data.status==0){
					layer.close(loadingdialog);
					dialog(data.msg,data.status);
				}else{
					layer.msg('同步成功',{offset:'100px'});
					layer.close(loadingdialog);
					tableIns.reload()
				}
			},'json');
		});
	}

	//导出
	function dataexcel(){
		var ids = '';

		var checkStatus = table.checkStatus('tabledata')
		var checkData = checkStatus.data; //得到选中的数据

		for(var i=0;i<checkData.length;i++){
			ids = ids+','+checkData[i]['id'];
		}

		datawhere.ids = ids;
		console.log(datawhere);
		//return false;
		window.location.href='{:url('excel')}'+urlEncode(datawhere)
	}
			function datadel(id){
				var ids = [];
				if(id==0){
					var checkStatus = table.checkStatus('tabledata')
					var checkData = checkStatus.data; //得到选中的数据
					if(checkData.length === 0){
						return layer.msg('请选择数据');
					}
					var ids = [];
					for(var i=0;i<checkData.length;i++){
						ids.push(checkData[i]['id']);
					}
				}else{
					ids.push(id)
				}
				layer.confirm('确定要删除吗?',{icon: 7, title:'操作确认'}, function(index){
					//do something
					layer.close(index);
					var index = layer.load();
					$.post("{:url('del')}",{ids:ids},function(data){
						layer.close(index);
						dialog(data.msg,data.status);
						tableIns.reload()
					})
				});
			}		
	</script>
	{include file="public/copyright"/}
</body>
</html>